package com.example.demo.core.controller;

import com.example.demo.util.PermissionUtil;
import com.example.demo.core.entity.Permission;
import com.example.demo.core.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
    * @Description: 获取权限列表
    * @Param:  无
    * @return:  权限列表
    * @Author: wangze
    */
    @PreAuthorize("hasPermission('powerList', 'users:permissions:list')")
    @GetMapping("/permissions/list")
    public List<Permission> getPermissionList(){
        return permissionService.getPermissionList();
    }

    /**
    * @Description: 获取权限树
    * @Param:  无
    * @return:  树形权限列表
    * @Author: wangze
    */
    @PreAuthorize("hasPermission('roleList', 'users:roles:allotpermissions')")
    @GetMapping("/permissions/tree")
    public List<Permission> getPermissionTree(){
        List<Permission> permissions = permissionService.getPermissionList();
        List<Permission>permissionList = new ArrayList<>(
                PermissionUtil.ListToTree(permissions)
        );
        return permissionList;
    }
}
